package com.bac.bacplatform;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.android.volley.Response.ErrorListener;
import com.android.volley.VolleyError;
import com.bac.http.VolleyErrorHelper;
import com.bac.http.VolleyErrorHelper.CommenErrorHandler;

/**
 * 基础Activity
 * 
 * @author lerry
 * 
 */
public abstract class BaseActivity extends AbstructCommonActivity implements ErrorListener, CommenErrorHandler{

	public abstract void init();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		BacApplication.getInstance().addActivity(this);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		init();
	}
	
	@Override
	protected void onDestroy() {
		Log.i(getClass().getName(), "---destroy");
		super.onDestroy();
	}

	@Override
	public void onErrorResponse(VolleyError error) {
//		dismissLoadingProgress();
		dismissLoadingProgressNow();
		VolleyErrorHelper.handleServerError(error, this);
	}

	/**
	 * 向左滑动进入
	 * @param intent
	 */
	public void startActivityIn(Intent intent) {
		
		startActivity(intent);
		
		overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
	}

	/**
	 * 向右滑动退出
	 */
	public void finishActivityOut(){
		
		finish();
		
		overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
	}
	
	@Override
	public void hanlderTimeoutError() {
		Log.e(getClass().getName(), "hanlderTimeoutError");
		showToast("连接超时,请稍后重试");
	}

	@Override
	public void hanlderErrorShowMessage(String string) {
		Log.e(getClass().getName(), "hanlderErrorShowMessage "+string);
		showAlertDialog("提示", string);
	}

	@Override
	public void hanlderAutorError() {
		Log.e(getClass().getName(), "hanlderAutorError");	
//		AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.Custom_dialog);
//		final AlertDialog alertDialog = builder.create();
//		alertDialog.setCanceledOnTouchOutside(false);
//		alertDialog.show();
//		Window window = alertDialog.getWindow();
//		View view = LayoutInflater.from(this).inflate(R.layout.dialog_alert, null);
//		view.findViewById(R.id.ly_cancel).setVisibility(View.VISIBLE);
//		view.findViewById(R.id.line).setVisibility(View.VISIBLE);
//		
//		((TextView)(view.findViewById(R.id.tv_title))).setText("提示");
//		TextView messageTV = (TextView)view.findViewById(R.id.tv_message);
//		messageTV.setText("现在就去登录");
//		
//		view.findViewById(R.id.ly_cancel).setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				alertDialog.dismiss();
//			}
//		});
//		
//		view.findViewById(R.id.ly_ok).setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				alertDialog.dismiss();
//				goToLogin();
//			}
//		});
//		window.setContentView(view);
//		window.setLayout((int)(Util.getWidth(getApplicationContext()) * 0.8), WindowManager.LayoutParams.WRAP_CONTENT);
		goToLogin();
	}

	@Override
	public void hanlderUnknowError() {
		Log.e(getClass().getName(), "hanlderUnknowError");		
		showToast("未知异常，请稍后重试");
	}

	@Override
	public void hanlderNetworkError() {
		Log.e(getClass().getName(), "hanlderNetworkError");	
		showToast("网络异常");
	}
	
	public void goToLogin(){
		Intent intent = new Intent(this, LoginActivity.class);
		startActivity(intent);
//		finish();
	}
}
